﻿//You can change this to feet your needs, just please keep giving credits to the author
//Thanks
//	DarkAngel  ( DarkAngel@IdleZone.Org )

using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;

namespace DarkRuler
{
	[Serializable()]
	public class ConfigItem
	{
		[System.Xml.Serialization.XmlAttribute()]
		public double BoxSizeInCm { get; set; }

		[System.Xml.Serialization.XmlAttribute()]
		public double BoxXSize_InPixel { get; set; }

		[System.Xml.Serialization.XmlAttribute()]
		public double BoxYSize_InPixel { get; set; }

		[System.Xml.Serialization.XmlAttribute()]
		public bool AutoUpdate { get; set; }

		[System.Xml.Serialization.XmlAttribute()]
		public bool UsePointForBox { get; set; }

		[System.Xml.Serialization.XmlAttribute()]
		public bool UseButtonsForZoom { get; set; }

		public static ConfigItem GetConfig()
		{
			ConfigItem config = new ConfigItem();
			System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(config.GetType());
			System.IO.Stream fs = new System.IO.StreamReader(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace(".exe", ".exe.config"), System.Text.Encoding.Default).BaseStream;
			System.Xml.XmlReader reader = new System.Xml.XmlTextReader(fs);
			config = (ConfigItem)serializer.Deserialize(reader);
			reader.Close();
			return config;
		}
		public static void SaveConfig(ConfigItem config)
		{
			System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(config.GetType());
			System.IO.Stream fs = new System.IO.StreamWriter(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace(".exe", ".exe.config"), false).BaseStream;
			System.Xml.XmlWriter writer = new System.Xml.XmlTextWriter(fs, System.Text.Encoding.Default);
			serializer.Serialize(writer, config);
			writer.Close();
		}
	}

	public class Config
	{
		private static Config _instance = new Config();
		private ConfigItem config;
		private Config()
		{
			config = new ConfigItem();
			config.BoxSizeInCm = 8.5;
			config.BoxXSize_InPixel = 85;
			config.BoxYSize_InPixel = 55;
			config.AutoUpdate = true;
			config.UseButtonsForZoom = false;
			config.UsePointForBox = false;
			if (!System.IO.File.Exists(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Replace(".exe", ".exe.config")))
			{
				ConfigItem.SaveConfig(config);
			}
			else
			{
				config = ConfigItem.GetConfig();
			}
		}

		public static void Save()
		{
			ConfigItem.SaveConfig(_instance.config);
		}

		public static void Reload()
		{
			//System.Reflection.Assembly.GetExecutingAssembly().GetName().co
			_instance.config = ConfigItem.GetConfig();
		}

		public static double BoxSizeInCm { get { return _instance.config.BoxSizeInCm; } set { _instance.config.BoxSizeInCm = value; } }
		public static double BoxXSize_InPixel { get { return _instance.config.BoxXSize_InPixel; } set { _instance.config.BoxXSize_InPixel = value; } }
		public static double BoxYSize_InPixel { get { return _instance.config.BoxYSize_InPixel; } set { _instance.config.BoxYSize_InPixel = value; } }
		public static bool AutoUpdate { get { return _instance.config.AutoUpdate; } set { _instance.config.AutoUpdate = value; } }
		public static bool UseButtonsForZoom { get { return _instance.config.UseButtonsForZoom; } set { _instance.config.UseButtonsForZoom = value; } }
		public static bool UsePointForBox { get { return _instance.config.UsePointForBox; } set { _instance.config.UsePointForBox = value; } }
	}
}
